<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src=""></script>
</head>

<body>
    <div>
        <h2>Promise 封装 ajax 操作</h2>
        <button id="btn">点击发送ajax请求</button>
    </div>
    <script>
        const btn = document.getElementById('btn')
        btn.onclick = () => {
            console.log('按钮被点击')
            let p = new Promise((resolve, reject) => {
                const xml = new XMLHttpRequest()
                xml.open('get', 'http://127.0.0.1:8000/server', true)
                xml.send()
                xml.onreadystatechange = function () {
                    if (xml.readyState === 4) {
                        if (xml.status >= 200 && xml.status < 300) {
                            resolve(xml.response)
                        } else {
                            reject(xml.status)
                        }
                    }
                }
            })
            p.then((res) => {
                console.log(res)
            }, (err) => {
                console.log(err, "---")
            })
        }
    </script>
</body>

</html>